//
//  ThirdViewController.m
//  项目A --半糖
//
//  Created by romana on 16/5/16.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "ThirdViewController.h"
#import "HeadCollectionview.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "ClassModel.h"
#import "CheckViewController.h"
#import "FootTableViewCell.h"
#import "HeadDetailsViewController.h"
#import "BaseTableView.h"
#import "SearchViewController.h"
#import "SecondTabHotViewController.h"
#import "BaseLabel.h"



#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface ThirdViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) HeadCollectionview *headView1;

@property(nonatomic,strong)BaseTableView *myTableView;
@property(nonatomic,strong)NSMutableArray *MyArray;

@property (nonatomic, strong) NSArray *titleArr; /** 分区头*/


@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  //  self.view.backgroundColor = [UIColor whiteColor];
        self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"分类";
    self.automaticallyAdjustsScrollViewInsets = NO;
    //右侧搜索
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(rightBarAction)];

    //左侧热门
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"热门" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction)];
    
    // tableView的建立
    [self creatTableView];
    // 头部轮播图(封装的view)
    self.headView1 = [[HeadCollectionview alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 5)];
    self.myTableView.tableHeaderView = self.headView1;
    //[self.view addSubview:self.headView1];
    
    // 数据处理
    self.MyArray = [NSMutableArray array];
    [self getData];
    // 分区头
    self.titleArr = @[@"对象",@"风格",@"品类"];
    //查看全部
    [self creataCheckAllButton];
    
    // 消息通知 (gift页面跳转详情的实现 及左右随动)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"push" object:nil];
    
    // Do any additional setup after loading the view.
    
    
}
-(void)leftBarButtonAction{

    SecondTabHotViewController *STHVC = [[SecondTabHotViewController alloc] init];
    [self.navigationController pushViewController:STHVC animated:YES];

}


-(void)rightBarAction{
    
    NSLog(@"1123123");
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];


}


-(void)creatTableView{
    BaseTableView *tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHT - 64 - 49 ) style:(UITableViewStylePlain)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    [tableView registerClass:[FootTableViewCell class] forCellReuseIdentifier:@"tableView"];
    self.myTableView = tableView;
    [self.view addSubview:self.myTableView];
}

#pragma mark 攻略
// 消息通知(头部详情)
-(void)notificationAction:(NSNotification *)noti{
    NSString *str = noti.userInfo[@"key"];
    HeadDetailsViewController *headDVC = [[HeadDetailsViewController alloc] init];
    headDVC.myId = str;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:headDVC];
    [self presentViewController:nav animated:YES completion:^{
        
    }];
    
}



-(void)getData{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://api.liwushuo.com/v2/channel_groups/all" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dataDic = responseObject[@"data"];
        NSArray *chanArr = dataDic[@"channel_groups"];
        self.MyArray = [ClassModel transformWithArray:chanArr];
        [self.myTableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];





}

-(void)creataCheckAllButton{
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn.frame = CGRectMake(WIDTH - 70, 0, 70, 40);
    btn.backgroundColor = [UIColor colorWithRed:234/ 255.0 green:248 / 255.0 blue:254 / 255.0 alpha:1];
    [self.myTableView.tableHeaderView addSubview:btn];
    [btn setTitle:@"查看全部" forState:(UIControlStateNormal)];
    [btn addTarget:self action:@selector(click) forControlEvents:(UIControlEventTouchUpInside)];
}
-(void)click{
    CheckViewController *checkVC = [[CheckViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:checkVC];
    [self presentViewController:nav animated:YES completion:^{
        
    }];
}

// 分区头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [self viewForHeaderView:section];
}
// 分区头加分类名称
-(UIView *)viewForHeaderView:(NSInteger)section{
    BaseLabel *lable = [[BaseLabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.myTableView.frame), 20)];
   // lable.backgroundColor = [UIColor clearColor];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.text = self.titleArr[section];
    lable.font = [UIFont systemFontOfSize:20];
    
    return lable;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableView" forIndexPath:indexPath];
    CGFloat h = [self tableView:tableView heightForRowAtIndexPath:indexPath];
    cell.collectionView.frame = CGRectMake(cell.collectionView.frame.origin.x, cell.collectionView.frame.origin.y, cell.collectionView.frame.size.width, h);
    ClassModel *c = self.MyArray[indexPath.section];;
    cell.cc = c;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
  //  cell.backgroundColor = [UIColor yellowColor];
    
    //    cell.delegate = self;
    //    tableView.rowHeight = HEIGHT/3;
    
    return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.MyArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

// tableView的自适应高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ClassModel *c = self.MyArray[indexPath.section];
    NSInteger count = c.channels.count;
    NSInteger remainder = count % 4;
    NSInteger result = count / 4;
    NSInteger lines = 0;
    if (remainder) {
        lines = result + 1;
    }else{
        lines = result;
    }
    return lines*(85 + 20);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
